package com.ssm.controller;

import com.ssm.dao.IBookDao;
import com.ssm.model.Book;
import com.ssm.model.Cart;
import com.ssm.model.User;
import com.ssm.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.jws.WebParam;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.sql.Date;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping("/s")
    public String selectAllBook(Model model) {
        List<Book> books = bookService.selectBookByAuthor("小明");
        model.addAttribute("books", books);
        return "book";
    }

    @RequestMapping("/selectBookByBook_id")
    public String selectBookByBookid(String book_id, Model model)
    {
        Book book  = bookService.selectBookByBook_id(book_id);
        List<Book> books = new ArrayList<>();
        books.add(book);
        model.addAttribute("books", books);
        return "book_manage";
    }

    @RequestMapping("/deleteBookByBook_id")
    public String deleteBookByBookid(String book_id)
    {
        bookService.deleteBook(book_id);
        return "redirect:/tobookmanage";
    }


    @RequestMapping("/tohome")
    public String tohome(Model model)
    {
        List<Book> books = bookService.selectAllBook();
        model.addAttribute("books", books);
        return "home";
    }


    @RequestMapping("/tobookmanage")
    public String tobookmanage(Model model)
    {
        List<Book> books = bookService.selectAllBook();
        model.addAttribute("books", books);
        return "book_manage";
    }

    @RequestMapping("/toaddbook")
    public String toaddbook()
    {
        return "add_book";
    }

    @RequestMapping(value = "/addbook", method = RequestMethod.POST)
    public String addbook(Book book)
    {
        bookService.addBook(book);
        return "redirect:/tobookmanage";
    }

    @RequestMapping("/toupdate_book")
    public String toupdatebook(String book_id, Model model)
    {
        Book book = bookService.selectBookByBook_id(book_id);
        model.addAttribute("book", book);
        return "update_book";
    }

    @RequestMapping(value = "/updatebook", method = RequestMethod.POST)
    public String updatebook(Book book)
    {
        bookService.updateBook(book);
        return "redirect:/tobookmanage";
    }

    @RequestMapping("/selectbooktohome")
    public String selectbooktohome(String name, Model model)
    {
        List<Book> books = bookService.selectBookByName(name);
        model.addAttribute("books", books);
        return "home";
    }

    @RequestMapping("/tobookdetail")
     public String tobookdetail(String book_id, Model model)
    {
        Book book = bookService.selectBookByBook_id(book_id);
        model.addAttribute("book", book);
        return "bookdetail";
    }

    @RequestMapping("/test")
    public String test()
    {
        return "test";
    }

    /*
    @RequestMapping("/uplod")
    public String test(String book_id, MultipartFile file)
    {
        String filename;
        File filePath = new File("C:\\Users\\Bronya\\Desktop\\数据库\\mybook\\src\\main\\webapp\\images\\");
        String filetype = file.getContentType();
        filename = book_id + filetype;
        try {
            file.transferTo(new File(filePath+filename));
        }
        catch (Exception exception)
        {

        }

        return "test";
    }*/

    @RequestMapping("addtoche")
    public String addtoche(Model model, HttpSession session)
    {
        String username = (String) session.getAttribute("username");
        String testusername = username;
        model.addAttribute("testusername", testusername);
        return "user";
    }

    @RequestMapping("/addtocart")
    public String addtocart(String book_id, HttpSession session, Model model)
    {
        String username = (String) session.getAttribute("username");
        Map<String, Integer> map= (Map<String, Integer>)session.getAttribute("map");
        Book book = bookService.selectBookByBook_id(book_id);
        /*List<Book> books = new ArrayList<>();
        books.add(book);
        model.addAttribute("books", books);
        return "book";*/

        if (!map.containsKey(book_id))
        {
            map.put(book_id, 1);
        }
        else
        {

            int cnt = map.get(book_id);
            map.put(book_id, ++cnt);
        }
        session.removeAttribute("map");
        session.setAttribute("map", map);
        return "redirect:/tocart";
    }

    @RequestMapping("/tocart")
    public String tocart(HttpSession session, Model model)
    {
        Map<String, Integer> map= (Map<String, Integer>)session.getAttribute("map");
        List<Cart> carts = new ArrayList<>();
        double total = 0;
        for (Map.Entry<String, Integer> entry : map.entrySet())
        {
            if (entry.getValue() > 0)
            {


            Book book = bookService.selectBookByBook_id(entry.getKey());
            Integer cnt = entry.getValue();
            String book_id = entry.getKey();
            double price = cnt * book.getPrice();
            total += price;
            Cart cart = new Cart(book_id, cnt, price, book.getName());
            carts.add(cart);
            }
        }


        model.addAttribute("carts", carts);
        session.setAttribute("carts", carts);
        model.addAttribute("total",  total);
        session.setAttribute("total", total);

        return "cart";
    }

    @RequestMapping("/tohuman")
    public String tohuam(Model model)
    {
        List<Book> books = bookService.selectBookByType("人文社科");
        model.addAttribute("books", books);
        return "home";
    }

    @RequestMapping("/tofinance")
    public String tofinance(Model model)
    {

            List<Book> books = bookService.selectBookByType("金融");
            model.addAttribute("books", books);
            return "home";
    }

    @RequestMapping("/tocomputer")
    public String tocomputer(Model model)
    {

        List<Book> books = bookService.selectBookByType("计算机");
        model.addAttribute("books", books);
        return "home";
    }

    @RequestMapping("/tochildren")
    public String tochildren(Model model)
    {

        List<Book> books = bookService.selectBookByType("儿童读物");
        model.addAttribute("books", books);
        return "home";
    }

    @RequestMapping("/addone")
    public String addone(String book_id, Model model, HttpSession session)
    {
        Map<String, Integer> map= (Map<String, Integer>)session.getAttribute("map");
        for (Map.Entry<String, Integer> entry : map.entrySet())
        {
                if(entry.getKey().equals(book_id))
                {
                    map.put(entry.getKey(), entry.getValue() + 1);
                }


        }
        session.removeAttribute("map");
        session.setAttribute("map", map);
        return "redirect:/tocart";
    }

    @RequestMapping("/releaseone")
    public String releaseone(String book_id, Model model, HttpSession session)
    {
        Map<String, Integer> map= (Map<String, Integer>)session.getAttribute("map");
        for (Map.Entry<String, Integer> entry : map.entrySet())
        {
            if(entry.getKey().equals(book_id))
            {
                map.put(entry.getKey(), entry.getValue() - 1);
            }

        }
        session.removeAttribute("map");
        session.setAttribute("map", map);
        return "redirect:/tocart";
    }

    @RequestMapping("/deleteone")
    public String deleteone(String book_id, Model model, HttpSession session)
    {
        Map<String, Integer> map= (Map<String, Integer>)session.getAttribute("map");
        for (Map.Entry<String, Integer> entry : map.entrySet())
        {
            if(entry.getKey().equals(book_id))
            {
                map.put(entry.getKey(), 0);
            }

        }
        session.removeAttribute("map");
        session.setAttribute("map", map);
        return "redirect:/tocart";
    }

    @RequestMapping("deleteall")
    public String deleteall(Model model, HttpSession session)
    {
        Map<String, Integer> map= (Map<String, Integer>)session.getAttribute("map");
        for (Map.Entry<String, Integer> entry : map.entrySet())
        {
                map.put(entry.getKey(), 0);
        }
        return "redirect:/tohome";
    }
}
